package net.ksic.uridongne;

import net.ksic.common.NetworkManager;
import net.ksic.common.PatternChecker;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SignupActivity extends Activity implements OnClickListener {

	TextView idView = null, pwView = null, nameView = null, telView = null,
			nickView = null, commentView = null, stdateView = null, modateView = null;
	
	EditText id = null, pw = null, name = null, telnumber = null,
			nickname = null, comment = null, stdate = null, modate = null;
	
	Button cancel = null, signup = null;

	TelephonyManager manager = null;
	NetworkManager networkManager = null;
	PatternChecker patternChecker;
	String phoneNumber = null;
	String RESULT = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signup);

		setButton();

		manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		networkManager = new NetworkManager();
		phoneNumber = manager.getLine1Number();

		telnumber.setText(phoneNumber);
	}

	public void setButton() {
		idView = (TextView) findViewById(R.id.idView);
		pwView = (TextView) findViewById(R.id.pwView);
		nameView = (TextView) findViewById(R.id.nameView);
		telView = (TextView) findViewById(R.id.telView);
		nickView = (TextView) findViewById(R.id.nickView);
		commentView = (TextView) findViewById(R.id.commentView);
		stdateView = (TextView) findViewById(R.id.stdateView);
		modateView = (TextView) findViewById(R.id.modateView);

		id = (EditText) findViewById(R.id.id);
		pw = (EditText) findViewById(R.id.pw);
		name = (EditText) findViewById(R.id.name);
		telnumber = (EditText) findViewById(R.id.telnumber);
		nickname = (EditText) findViewById(R.id.nickname);
		comment = (EditText) findViewById(R.id.comment);
		stdateView = (EditText) findViewById(R.id.stdate);
		modateView = (EditText) findViewById(R.id.modate);

		cancel = (Button) findViewById(R.id.cancel);
		signup = (Button) findViewById(R.id.signup);

		cancel.setOnClickListener(this);
		signup.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			callIntro();
			break;
		case R.id.signup:
			Log.i("id.getText().toString()", id.getText().toString());
			Log.i("pw.getText().toString()", pw.getText().toString());
			Log.i("name.getText().toString()", name.getText().toString());
			Log.i("telnumber.getText().toString()", telnumber.getText()
					.toString());
			Log.i("nickname.getText().toString()", nickname.getText()
					.toString());
			Log.i("comment.getText().toString()", comment.getText().toString());
			Log.i("stdateView.getText().toString()", stdateView.getText().toString());
			Log.i("modateView.getText().toString()", modateView.getText().toString());

			if (id.getText().toString().equals("")
					|| pw.getText().toString().equals("")
					|| name.getText().toString().equals("")
					|| telnumber.getText().toString().equals("")
					|| nickname.getText().toString().equals("")
					|| comment.getText().toString().equals("")
					|| stdateView.getText().toString().equals("")
					|| modateView.getText().toString().equals("")) {

				Toast.makeText(this, "모든정보를 입력해주세요.", Toast.LENGTH_SHORT)
						.show();
			} else {
				if (patternChecker.getInstance().isEngNum(id.getText().toString())) {
					String URL = "http://20.20.20.124:8080/MyServer/Signup.jsp";
					//String URL = "http://20.20.20.34:8080/lookdom/testsignup.do";
					
					String MSG = id.getText().toString() + ","
							+ pw.getText().toString() + ","
							+ name.getText().toString() + ","
							+ nickname.getText().toString() + ","
							+ telnumber.getText().toString() + ","
							+ comment.getText().toString() + ","	
							+ stdateView.getText().toString() + ","	
							+ modateView.getText().toString();

					new sendWorker().execute(URL, MSG);

				} else {
					Toast.makeText(this, "아이디는 영어와 숫자만 가능합니다.", Toast.LENGTH_SHORT)
					.show();
				}				
			}			
			break;
		}
	}

	private class sendWorker extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			
			Log.i("URL?MSG", params[0] + "?" + params[1]);

			RESULT = networkManager.signupJSP(params[0], params[1]);			

			return RESULT;
		}

		@Override
		protected void onPostExecute(String result) {
			Log.i("RESULT-_-", RESULT);
			if (RESULT.equals("true")) {
				callLogin();
			} else {
				callIntro();
			}
		}
	}

	private void callIntro() {
		Intent intent = null;
		intent = new Intent(this, IntroActivity.class);
		startActivity(intent);
	}

	private void callLogin() {
		Intent intent = null;
		intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
	}
}
